<template>
	<view>
		<!-- 选择收货地址的盒子 -->
		<view class="address-choose-box" v-if="Object.keys(address).length===0">
			<button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址+</button>
		</view>
		<!-- 渲染收货信息的盒子 -->
		<view class="address-info-box" v-else @click="chooseAddress">

			<view class="row1">
				<view class="row1-left">
					<view class="username">收货人：<text>{{address.userName}}</text></view>
				</view>
				<view class="row1-right">
					<view class="phone">电话：<text>{{address.telNumber}}</text></view>
					<uni-icons type="arrowright" size="16"></uni-icons>
				</view>
			</view>

			<view class="row2">
				<view class="row2-left">收货地址：</view>
				<view class="row2-right">{{addstr}}</view>
			</view>
		</view>

		<!-- 底部的边框线 -->
		<!-- <image src="/static/cart_border@2x.png" class="address-border"></image> -->
		<view class="address-border">

		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'


	export default {
		name: "my-address",
		data() {
			return {
				// 收货地址，调用微信API获取
				// address: {},
			};
		},
		computed: {
			// 映射vuex中用户收获地址
			...mapState('m_user', ['address']),
			// 映射vuex中 getter 中处理好的收获地址
			...mapGetters('m_user', ['addstr']),
			// addstr() {
			// 	// 收获地址的拼接
			// 	if (JSON.stringify(this.address) === '{}') return ''

			// 	// 拼接 省，市，区，详细地址 的字符串并返回给用户
			// 	return this.address.provinceName + this.address.cityName + this.address.countyName + this.address
			// 		.detailInfo
			// }
		},
		methods: {
			...mapMutations('m_user', ['updateAddress']),
			// 从微信获取用户收获地址
			async chooseAddress() {
				// https://developers.weixin.qq.com/miniprogram/dev/api/open-api/address/wx.chooseAddress.html
				// 调用的微信API获取
				const [err, succ] = await uni.chooseAddress()
				console.log(succ)
				// 判断数据异常
				if (!err) {
					// 设置页面数据
					// this.address = succ

					// 保存到vuex
					this.updateAddress(succ)
				}

			}
		}
	}
</script>

<style lang="scss">
	.address-choose-box {
		height: 90px;
		display: flex;
		justify-content: center;
		align-items: center;

	}

	.address-info-box {
		font-size: 12px;
		height: 90px;
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 0 5px;

		.row1 {
			display: flex;
			justify-content: space-between;
			margin-bottom: 10px;

			.row1-right {
				display: flex;

				.phone {
					margin-right: 5px;
				}
			}
		}

		.row2 {
			display: flex;
		}
	}

	.address-border {
		width: 100%;
		height: 5px;
		background: linear-gradient(135deg, #FBEC6B, #F4BC6B, #EB8B6B, #E0536B, #D6306D, #A32C68, #722A65, #422662, #12255D, #275B75, #3C918B, #52C6A2, #66F8B6);
	}
</style>
